﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using Ninject;
using Wicresoft.EKA.ITSM.BusinessImpl;

namespace Wicresoft.EKA.ITSM.Web.WebPortal.Controllers
{
    public class InspectionTicketAPIController : BaseAPIController
    {
        [Inject]
        public ISMGXJPlanObject SMGXJPlanObject { get; set; }

        [Inject]
        public ISMGXJPlanRule SMGXJPlanRule { get; set; }

        [Inject]
        public ISMGXJPlanTicket SMGXJPlanTicket { get; set; }

        [Inject]
        public ISMGXJPlanTaskFrame SMGXJPlanTaskFrame { get; set; }

        [Inject]
        public IUser User { get; set; }

        [Inject]
        public override IUserInfo UserInfo { get; set; }  //每个Controller必须包含的。

        protected override void Init()
        {
            ((IBase)SMGXJPlanObject).UserIdentify = UserName;
            ((IBase)SMGXJPlanRule).UserIdentify = UserName;
            ((IBase)SMGXJPlanTicket).UserIdentify = UserName;
            ((IBase)SMGXJPlanTaskFrame).UserIdentify = UserName;
            ((IBase)User).UserIdentify = UserName;
            base.Init();
        }

        protected override void Dispose(bool disposing)
        {
            ((IBase)SMGXJPlanObject).Dispose();
            ((IBase)SMGXJPlanRule).Dispose();
            ((IBase)SMGXJPlanTicket).Dispose();
            ((IBase)SMGXJPlanTaskFrame).Dispose();
            ((IBase)User).Dispose();
            base.Dispose(disposing);
        }

        //新建模板
        public object Post([FromBody] SMGXJPlanTicketModel model)
        {
            Init();
            string result = string.Empty;
            model.Submitter = UserName;
            model.ShortDescription = "Create";
            model.cChr_MarkAsDelete = "No";
            return SMGXJPlanTicket.Create(model);
        }

        //更新Ticket模板信息
        public object Put(string id,[FromBody] SMGXJPlanTicketModel model)
        {
            Init();
            string result = string.Empty;
            model.ShortDescription = "Modify";
            return SMGXJPlanTicket.Modify(id,model);
        }

        //通过ID获取Ticket信息，打开Ticket模板页
        public object GetbyID(string TicketID)
        {
            Init();
            return SMGXJPlanTicket.GetbyID(TicketID);
        }

        //获取所有模板信息
        public object GetAll(string cChr_office)
        {
            Init();
            string Qual = "";
            //List<SMGXJGroupAssoModel> listRoles = User.GetGroupAsso(UserName,"Yes");
            //if (listRoles.Count > 0)
            //{
            //    Qual = "AND (";
            //    foreach (var i in listRoles.Select(o => o.GroupID))
            //    {
            //        Qual = Qual + "'cChr_Office' = \"" + i + "\" OR";
            //    }
            //    Qual = Qual.TrimEnd('R').TrimEnd('O') + ")";
            //}
            if (cChr_office == "public")
            {
                Qual = Qual + "AND 'cChr_Department' = \"" + cChr_office + "\"";
            }
            if (cChr_office != "public")
            {
                Qual = Qual + "AND'cChr_Office' = \"" + cChr_office + "\"";
            }
            return SMGXJPlanTicket.GetList("'cChr_MarkAsDelete' = \"No\"" + Qual);
        }

        //另存功能
        public object GettoSaveAS(string TicketID, string Type)
        {
            Init();
            try
            {
                if (Type == "SaveAs")
                {
                    SMGXJPlanTicketModel ticket = new SMGXJPlanTicketModel();
                    var model = SMGXJPlanTicket.GetbyID(TicketID);
                    ticket.cChr_Department = model.cChr_Department;
                    ticket.cChr_Description = model.cChr_Description;
                    ticket.cChr_FullName = model.cChr_FullName;
                    ticket.cChr_MarkAsDelete = model.cChr_MarkAsDelete;
                    ticket.cChr_Name = model.cChr_Name + " Copy By " +TicketID;
                    ticket.cChr_Office = model.cChr_Office;
                    ticket.cChr_TicketID = SMGXJPlanTicket.GetTicketID();
                    ticket.Submitter = UserName;
                    ticket.ShortDescription = TicketID;
                    SMGXJPlanTicket.Create(ticket);

                    List<SMGXJPlanObjectModel> Tobjectlist = new List<SMGXJPlanObjectModel>();
                    var model2 = SMGXJPlanObject.GetList("'cChr_MarkAsDelete' = \"No\" AND 'cChr_RelatedTicketID' = \"" + TicketID + "\"").ToList();
                    foreach (var i in model2)
                    {
                        Tobjectlist.Add(new SMGXJPlanObjectModel
                        {
                            Submitter = UserName,
                            ShortDescription = "Save As by " + i.ShortDescription,
                            cChr_RelatedTicketID = ticket.cChr_TicketID,
                            cChr_OrderID = i.cChr_OrderID,
                            cChr_MarkAsDelete = "No",
                            cChr_Classification = i.cChr_Classification,
                            cChr_DeviceName = i.cChr_DeviceName,
                            cChr_Indicators = i.cChr_Indicators,
                            cChr_IndicatorIDs = i.cChr_IndicatorIDs,
                            cChr_Type = i.cChr_Type,
                            cChr_Location = i.cChr_Location,
                            cChr_IP = i.cChr_IP,
                            cChr_Hostname = i.cChr_Hostname,
                            cChr_Use = i.cChr_Use,
                            cChr_SQLVersion = i.cChr_SQLVersion,
                            cChr_DiskLable = i.cChr_DiskLable,
                            cChr_Char01 = i.cChr_Char01,
                            cChr_Char02 = i.cChr_Char02,
                            cChr_Char03 = i.cChr_Char03 
                        });
                    }
                    if(Tobjectlist.Count()>0){
                        SMGXJPlanObject.Createlist(Tobjectlist);
                    }
                    return ticket.cChr_TicketID;
                }
                else return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        //删除功能
        public object PUT(string TicketID)
        {
            Init();
            SMGXJPlanTicketModel model = new SMGXJPlanTicketModel();
            model.cChr_MarkAsDelete = "Yes";
            var id = SMGXJPlanTicket.GetbyID(TicketID).RequestID;
            return SMGXJPlanTicket.Modify(id, model);
        }
    }
}
